from PIL import Image
import os

IMAGES_FORMAT = ['.jpg', '.gif']
ims = [Image.open('./images/%s' % fn) for fn in os.listdir('./images/') for item in IMAGES_FORMAT if os.path.splitext(fn)[1] == item]
ims_size = [list(im.size) for im in ims]

middle_width = sorted(ims_size, key=lambda im: im[0])[int(len(ims_size)/2)][0]

for i in range(len(ims_size)):
    rate = middle_width/ims_size[i][0]
    ims_size[i][0] = middle_width
    ims_size[i][1] = int(rate*ims_size[i][1])
 
sum_height = sum([im[1] for im in ims_size])
# 创建空白长图 'RGB' - jpg
result = Image.new(ims[0].mode, (middle_width, sum_height))
 
# 拼接
top = 0
for i, im in enumerate(ims):
    mew_im = im.resize(ims_size[i], Image.ANTIALIAS)  # 等比缩放
    result.paste(mew_im, box=(0, top))
    top += ims_size[i][1]
# 保存
result.save('result.png')


图片压缩：https://www.cnblogs.com/li1992/p/10675769.html

